package com.WOWTradeNews.phone;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.WOWTradeNewsTest.R;
import com.WOWTradeNews.DataBase.ConstValues;
import com.WOWTradeNews.DataBase.Country;
import com.WOWTradeNews.DataBase.DBManager;
import com.WOWTradeNews.DataBase.SystemDBManager;
import com.WOWTradeNews.services.NewsServer;

public class ReCountry extends Activity implements OnClickListener{
	private String iid;
	private String country;
	
	private List<Country> myList;
	private Map<String, String> map;
	private List<TextView> viewList;
	
	private TextView nameTextView;
	private Button backButton;
	
	private LinearLayout linearLayout;
	private SystemDBManager systemDBManager;
	TextView top_title;
	
	boolean ct = false;
	
	public static int SETOF = 100000;
	
	DBManager dbManager;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.re_country);
        systemDBManager = new SystemDBManager(this);
        
        dbManager = new DBManager(this, ConstValues.DB_NEWS);
        
        Bundle bundle = this.getIntent().getExtras();
        iid = bundle.getString("iid");
        country = bundle.getString("country");
        ct = bundle.getBoolean("ct");
        top_title = (TextView) findViewById(R.id.top_title);
        if(ct) {
        	top_title.setText("經貿詞彙");
        }
        
        linearLayout = (LinearLayout) findViewById(R.id.re_country_lin);
        nameTextView = (TextView) findViewById(R.id.cty_name);
        if(country == null || country.equals("null") || country.equals("")) {
        	nameTextView.setText("無此相關國家");
        } else {
        	nameTextView.setText(country);
        }
        //nameTextView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        nameTextView.setTextColor(Color.BLACK);
        nameTextView.setTextSize(20);
        
        backButton = (Button) findViewById(R.id.back);
        backButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
        
        try {
        	if(ct) {
        		myList = NewsServer.getCT(country);
        	} else {
        		if(iid != null && !iid.equals("null") && !iid.equals("")) {
        			myList = NewsServer.getCountries(iid);
        		}
        	}
			Log.d("zhaosdfa", "myList size: " + myList.size());
		} catch (Exception e) {
			nameTextView.setText("無此相關國家");
			e.printStackTrace();
		}
        map = new HashMap<String, String>();
        viewList = new ArrayList<TextView>();
        if(myList != null) {
        	if(myList.size() == 0) {
        		nameTextView.setText("無此相關國家");
        	}
        	for(Country cty : myList) {
        		map.put(cty.getName(), cty.getContents());
        		TextView textView = new TextView(ReCountry.this);
        		textView.setText(cty.getName());
        		textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        		textView.setTextColor(Color.BLUE);
        		textView.setTextSize(18);
        		textView.setId(Integer.valueOf(cty.getIid()));
        		textView.setOnClickListener(this);
        		viewList.add(textView);
        		LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
        				   LinearLayout.LayoutParams.WRAP_CONTENT,
        				   LinearLayout.LayoutParams.WRAP_CONTENT
        				  ); 
        		textView.setLayoutParams(params);
        		LinearLayout lin = new LinearLayout(ReCountry.this);
        		LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
        				LinearLayout.LayoutParams.FILL_PARENT,
        				20);
        		lin.setLayoutParams(p);
        		linearLayout.addView(lin);
        		linearLayout.addView(textView);
        	}
        } else {
        	nameTextView.setText("無此相關國家");
        	Toast.makeText(ReCountry.this, "目前無資料", Toast.LENGTH_SHORT).show();
        }
	}

	@Override
	public void onClick(View view) {
//		if(!ct) {
			int nowCoin = systemDBManager.getMyCoin();
			
			AlertDialog.Builder builder = new AlertDialog.Builder(ReCountry.this);
			//您當前點數爲：收藏講扣3點。您當前點數不足，請先購買。
			builder.setTitle("提示");
			int iid = view.getId();
			if(!ct) {
				iid = iid - SETOF;
			} else {
				iid = iid + SETOF;
			}
//			if(dbManager.findByIid(iid + "") == null) {  //null表示是新闻
//			}
			if(systemDBManager.inMyFavoritesLsit(iid + "")) {
				String cty = ((TextView)view).getText().toString();
				String content = map.get(cty);
				Intent intent = new Intent(ReCountry.this, ReCountryDetail.class);
				Bundle bundle = new Bundle();
				bundle.putString("content", content);
				bundle.putString("title", cty);
				intent.putExtras(bundle);
//				startActivity(intent);
				Toast.makeText(ReCountry.this, "已加入我的最愛", 1).show();
			} else {
				if(nowCoin >= NewsDetail.COST_COIN) {
					Toast.makeText(ReCountry.this, "收藏成功，請到我的最愛查看", 1).show();
					String cty = ((TextView)view).getText().toString();
					String content = map.get(cty);
					systemDBManager.setMyCoin(nowCoin - NewsDetail.COST_COIN);
					systemDBManager.addFavorites(iid + "", content,cty);
					Intent intent = new Intent(ReCountry.this, ReCountryDetail.class);
					Bundle bundle = new Bundle();
					bundle.putString("content", content);
					bundle.putString("title", cty);
					intent.putExtras(bundle);
//					startActivity(intent);
				} else {
					String t2 = String.format("您當前點數(%d)不足，請先購買", nowCoin);
					builder.setMessage(t2);
					builder.setPositiveButton("確定", new DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent(ReCountry.this, Billing.class);
							startActivity(intent);
						}
						
					});
					builder.setNegativeButton("取消", null);
					builder.show();
				}
				
			}
		/*} else {
			String cty = ((TextView)view).getText().toString();
			String content = map.get(cty);
			Intent intent = new Intent(ReCountry.this, ReCountryDetail.class);
			Bundle bundle = new Bundle();
			bundle.putString("content", content);
			bundle.putString("title", cty);
			intent.putExtras(bundle);
			startActivity(intent);
		}*/
		
	}
	
}
